package com.innovation.chengzhangriji.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.BabyAndParent;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.MessageResponse;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.InviteShareActivity;
import com.innovation.chengzhangriji.ui.SuperTipDialogActivity;
import com.innovation.chengzhangriji.ui.TipDialogActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;

public class MessageAdapter extends RecyclerView.Adapter<MessageViewHolder> implements MessageViewHolder.OnClickMessageListener {

    private static final String MSG_NULL = "";

    private static final String MSG_1 = "点击消息进行确认";

    private static final String MSG_2 = "点击消息快去通知他加入吧!";

    private static final String MSG_3 = "点击消息可以查看您的特权哦!";

    private ArrayList<MessageResponse> mList;

    private Context mContext;

    private long mServerTS;

    public MessageAdapter(Context context) {
        mContext = context;
        mList = new ArrayList<>();
    }

    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
        MessageViewHolder viewHolder = new MessageViewHolder(view);
        viewHolder.setOnClickMessageListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MessageViewHolder holder, int position) {
        holder.setName(mList.get(position).getBabyName());
        holder.setRole(mList.get(position).getName());
        int babyId = mList.get(position).getBabyID();
        Baby baby = DatabaseManager.getInstance(mContext).queryBaby(babyId);
        if(baby == null) {
            holder.setImageUrl(mList.get(position).getLogoPath(), mList.get(position).getSex());
        } else {
            holder.setImageUrl(mList.get(position).getLogoPath(), baby.getGender());
        }
        holder.setContent(mList.get(position).getMessage());
        holder.setHandle(null);
        holder.setConfirm(MSG_NULL);
        holder.invisibleNew();

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.SIMPLIFIED_CHINESE);
        Date date = new Date(mList.get(position).getServerTS());
        holder.setTime(sf.format(date));

        if (mServerTS < mList.get(position).getServerTS()) {
            holder.visibleNew();
        } else {
            holder.invisibleNew();
        }

        switch (mList.get(position).getType()) {
            case 1:// 申请
                holder.setMsg("申请" + '\n' + "加入");
                if (mList.get(position).getApplyState() != 0) {
                    holder.setHandle(mContext.getResources().getDrawable(R.drawable.ic_handle));
                    holder.setConfirm(MSG_NULL);
                } else {
                    holder.setHandle(null);
                    holder.setConfirm(MSG_1);
                }
                break;
            case 2:// 欢迎
                holder.setMsg("热烈" + '\n' + "欢迎");
                // 判断欢迎类型
                LoginInfo loginInfo = Config.getLoginInfo(mContext);
                if (loginInfo == null) {
                    return;
                }
                // 查询权限
                String userId = loginInfo.getUserID();
                BabyAndParent babyAndParent = DatabaseManager.getInstance(mContext).queryBabyAndParent(userId, babyId);
                if (babyAndParent != null && babyAndParent.getRole() == 2) { // 超级管理员
                    holder.setConfirm("作为大家长,您还可以...");
                } else {
                    if (baby == null) {
                        holder.setConfirm("还有一个小秘密要告诉你");
                    } else {
                        String parentName = baby.getParentName();
                        if (parentName.equals(mList.get(position).getName())) { // 加入的人
                            holder.setConfirm("还有一个小秘密要告诉你");
                        } else {
                            holder.setConfirm(MSG_NULL);
                        }
                    }
                }
                break;
            case 3:// 取消
                holder.setMsg("取消" + '\n' + "关注");
                holder.setConfirm(MSG_NULL);
                break;
            case 4:// 受邀
                holder.setRole(mList.get(position).getBeInviteMemberName());
                holder.setMsg("邀请" + '\n' + "关注");
                switch (mList.get(position).getInvitationState()) {
                    case 1:
                        holder.setHandle(null);
                        holder.setConfirm(MSG_1);
                        break;
                    case 2:
                        holder.setHandle(mContext.getResources().getDrawable(R.drawable.ic_handle_agree));
                        holder.setConfirm(MSG_NULL);
                        break;
                    case 3:
                        holder.setHandle(mContext.getResources().getDrawable(R.drawable.ic_handle_refuse));
                        holder.setConfirm(MSG_NULL);
                        break;
                }
                break;
            case 5:// 受邀待确认
                holder.setRole(mList.get(position).getBeInviteMemberName());
                holder.setMsg("邀请" + '\n' + "确认");
                switch (mList.get(position).getInvitationState()) {
                    case 0:
                        holder.setConfirm(MSG_1);
                        break;
                    case 1:
                        holder.setHandle(mContext.getResources().getDrawable(R.drawable.ic_handle));
                        break;
                    case 3:
                        holder.setHandle(mContext.getResources().getDrawable(R.drawable.ic_handle));
                        break;
                }
                break;
            case 6:// 删除关注人
                holder.setRole(mList.get(position).getDeleteMemberName());
                holder.setMsg("取消" + '\n' + "关注");
                holder.setConfirm(MSG_NULL);
                break;
            case 7:// 邀请已确认
                holder.setRole(mList.get(position).getName());
                holder.setMsg("邀请" + '\n' + "批准");
                holder.setConfirm(MSG_2);
                break;
            case 8:// 角色变更
                holder.setRole(mList.get(position).getChangeMemberName());
                holder.setMsg("角色" + '\n' + "变更");
                if (mList.get(position).getRoleChangeType() != 3) {
                    holder.setConfirm(MSG_3);
                } else {
                    holder.setConfirm(MSG_NULL);
                }
                break;
            case 9:// 权限变更
                holder.setRole(mList.get(position).getChangeMemberName());
                holder.setMsg("变更" + '\n' + "通知");
                holder.setConfirm(MSG_NULL);
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }
        return mList.size();
    }

    @Override
    public void onClickMessage(MessageViewHolder viewHolder) {
        int position = viewHolder.getLayoutPosition();
        switch (mList.get(position).getType()) {
            case 1:// 申请
                if (mList.get(position).getApplyState() == 0) {
                    superTip(0, position);
                }
                break;
            case 2:// 欢迎
                // 判断欢迎类型
                LoginInfo loginInfo = Config.getLoginInfo(mContext);
                if (loginInfo == null) {
                    return;
                }
                // 查询权限
                String userId = loginInfo.getUserID();
                int babyId = mList.get(position).getBabyID();
                BabyAndParent babyAndParent = DatabaseManager.getInstance(mContext).queryBabyAndParent(userId, babyId);
                if (babyAndParent != null && babyAndParent.getRole() == 2) { // 超级管理员
                    welcomeTip(0);
                } else {
                    Baby baby = DatabaseManager.getInstance(mContext).queryBaby(userId, babyId);
                    if (baby == null) {
                        welcomeTip(1);
                    } else {
                        String parentName = baby.getParentName();
                        if (parentName.equals(mList.get(position).getName())) { // 加入的人
                            welcomeTip(1);
                        } else {
                            return;
                        }
                    }
                }
                break;
            case 3:// 取消
                break;
            case 4:// 受邀
                if (mList.get(position).getInvitationState() == 1) {
                    superTip(1, position);
                }
                break;
            case 5:// 受邀待确认
                if (mList.get(position).getInvitationState() == 0) {
                    superTip(2, position);
                }
                break;
            case 6:// 删除关注人
                break;
            case 7:// 邀请已确认
                invitation(position);
                break;
            case 8:// 角色变更
                switch (mList.get(position).getRoleChangeType()) {
                    case 1: // 变超管
                        tip(1);
                        break;
                    case 2: // 成员变管理
                        tip(2);
                        break;
                    case 3: // 管理变成员
                        break;
                }
                break;
            case 9:// 权限变更
                switch (mList.get(position).getPowerChangeType()) {
                    case 1:
                        tip(3);
                        break;
                }
                break;
        }
    }

    public void setServerTS(long serverTS) {
        mServerTS = serverTS;
    }

    public void set(MessageResponse[] messages) {
        mList.clear();
        Collections.addAll(mList, messages);
        notifyDataSetChanged();
    }

    private void welcomeTip(int welcomeType) {
        Intent intent = new Intent(mContext, TipDialogActivity.class);
        intent.putExtra("state", 0);
        intent.putExtra("welcome", welcomeType);
        mContext.startActivity(intent);
    }

    /**
     * @param i 0 欢迎 1 超级管理员 2 管理员 3 一起记
     */
    private void tip(int i) {
        Intent intent = new Intent(mContext, TipDialogActivity.class);
        intent.putExtra("state", i);
        mContext.startActivity(intent);
    }

    /**
     * @param i        0 申请加入 1 邀请关注 2 邀请确认
     * @param position 位置
     */
    private void superTip(int i, int position) {
        Intent intent = new Intent(mContext, SuperTipDialogActivity.class);
        intent.putExtra("state", i);
        intent.putExtra("message", mList.get(position).getMessage());
        intent.putExtra("headUrl", mList.get(position).getLogoPath());
        intent.putExtra("sex", mList.get(position).getSex());
        intent.putExtra("babyID", mList.get(position).getBabyID());
        intent.putExtra("applyID", mList.get(position).getApplyID());
        intent.putExtra("invitationID", mList.get(position).getInvitationID());
        intent.putExtra("baby", mList.get(position).getBabyName());
        intent.putExtra("role", mList.get(position).getName());
        intent.putExtra("phone", mList.get(position).getApplyPhone());
        ((Activity) mContext).startActivityForResult(intent, 1);
    }

    /**
     * 邀请
     *
     * @param position 位置
     */
    private void invitation(int position) {
        Intent intent = new Intent(mContext, InviteShareActivity.class);
        intent.putExtra("shareMsg", mList.get(position).getShareMessage());
        mContext.startActivity(intent);
    }
}
